/* 

This do file takes inputs of 

1. type1 (type of corporate transactions)
2. type2 (type of opposite corporate transactions)
3. begin (beginning of the sample period)
4. endd (end of the sample period)

and creates Figure 1, 2, or 3

*/

args type1 type2 begin endd

* Saving location
clear 
gen q = .
save "${root}\Data\temp", replace

forvalues t = -7/6{
	
	use "${root}\Data\corp_trade_data", clear
	
	
	if `t' < 0{
	
		* Relevant transaction
		gen transaction = 0
		bysort permno (date): replace transaction = 1 if `type1'[_n-`t'] == 1 
		bysort permno (date): replace transaction = 0 if `type2'[_n+1] == 1 | `type2'[_n+2] == 1 | `type2'[_n+3] == 1 | `type2'[_n+4] == 1 | `type2'[_n+5] == 1 | `type2'[_n+6] == 1
		keep if transaction == 1
	
	}
	else{
		
		* Relevant transaction
		gen transaction = 0
		bysort permno (date): replace transaction = 1 if `type1'[_n-`t'] == 1  
		bysort permno (date): replace transaction = 0 if `type2'[_n-1] == 1 | `type2'[_n-2] == 1 | `type2'[_n-3] == 1 | `type2'[_n-4] == 1 | `type2'[_n-5] == 1 | `type2'[_n-6] == 1
		keep if transaction == 1
	
	}
	
	* Quintiles
	astile q_size = lagged_mktcap, by(date) nq(5)
	drop if q_size == .
	astile q_composite = composite, by(date q_size) nq(5)
	drop if q_composite == .

	* Sample period
	keep if year >= `begin' & year <= `endd'

	* EW
	egen ret_e = mean(ret), by(q_composite date)

	* VW
	forvalues i = 1/5{
		bysort date: asgen temp_ret_`i' = (ret/(q_composite == `i')), w(lagged_mktcap)
	}
	gen ret_w = .
	forvalues i = 1/5{
		replace ret_w = temp_ret_`i' if q_composite == `i'
	}

	* # stocks
	egen num_stocks = count(permno), by(q_composite date)
	
	keep date year month q_composite ret_e ret_w num_stocks
	duplicates drop date q_composite, force

	* Bring factors
	merge m:1 year month using "${root}\Data\risk_factors"
	drop if _merge == 2
	drop _merge

	* Excess returns
	gen exret_e = ret_e - rf
	gen exret_w = ret_w - rf

	* Sqrt(n)
	gen sqrt_n = sqrt(num_stocks)
	
	* Compute alphas
	
	* EW
	forvalues j = 1/5{

		reg exret_e mktrf smb hml umd if q_composite == `j', robust
		local alpha_e_`j'= _b[_cons]

	}
	
	* VW
	forvalues j = 1/5{

		reg exret_w mktrf smb hml umd if q_composite == `j', robust
		local alpha_w_`j'= _b[_cons]

	}	
	
	* Generate data
	clear 
	set obs 5
	gen q = .
	gen t = `t'
	gen alpha_e = .
	gen alpha_w = .
	
	forvalues k = 1/5{
	
		replace q = `k' in `k'
		replace alpha_e = `alpha_e_`k'' in `k'
		replace alpha_w = `alpha_w_`k'' in `k'
	
	}
	
	append using "${root}\Data\temp"
	save "${root}\Data\temp", replace
	
}

* Chart
use "${root}\Data\temp", clear
bysort q (t): gen CAR_e = sum(alpha_e)
bysort q (t): gen CAR_w = sum(alpha_w)
replace CAR_e = 0 if t == -7
replace CAR_w = 0 if t == -7


* EW
twoway (line CAR_e t if q == 1, lc(b) lp(shortdash) lw(medthick) leg(label(1 "Q1") cols(1) ring(0) position(8) bmargin(0.5) region(lwidth(none) fc(none)))) ///
       (line CAR_e t if q == 2, lc(b) lp(dash_dot) lw(medthick) leg(label(2 "Q2"))) ///
	   (line CAR_e t if q == 3, lc(b) lp(dot) lw(medthick) leg(label(3 "Q3"))) ///
	   (line CAR_e t if q == 4, lc(b) lp(dash) lw(medthick) leg(label(4 "Q4"))) ///
	   (line CAR_e t if q == 5, lc(b) lp(solid) lw(medthick) leg(label(5 "Q5")) ///
	   xtitle("Trading Months Relative to Launch Date") ytitle("Cumulative Abnormal Returns") /// 
	   xlab(-6(3)6)) ///

*	   

capture erase "${root}\Data\temp.dta"











